package com.nf.service;

import com.nf.dao.UserDao;
import com.nf.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Scope("prototype")
public class UserServiceImpl implements UserService {

    @Autowired
    private UserDao userDao;

    @Transactional
    public List<User> getAllUser() {
        List<User> userList = userDao.getAllUser();
        //编写游戏规则（业务逻辑代码：没有任何数据库操作）
        for (User u:userList){
            if( u.getAge()>=18  ){
                u.setStatus("成年人");
            }
            else {
                u.setStatus("小朋友");
            }
        }
        return userList;
    }

    @Transactional
    public boolean isWin(Integer id) {
        User user = userDao.getUser(id);
        //如果查询不到用户，直接返回false
        if(user==null){
            return false;
        }
        //以下是有用户
        //如果不是男，返回false
        if (user.getSex()!=1){
            return false;
        }
        //剩下的都是女的
        if (user.getAge()>=18||user.getAge()<=15){
            return true;
        }
        else {
            return false;
        }
    }
}
